<template>
	<view>
		<my-search @myclick="gotoSearch"></my-search>
		<view class="cate-container">
			<scroll-view :scroll-top="scrollTop" scroll-y="true" :style="{'height': wh + 'px'}" class="cate-left">
				<view @click="choice(item, i)" class="left-item" :class="{'active': i===active}" v-for="(item, i) in cateLeftList" :key="i">
					{{item.cat_name}}
				</view>
			</scroll-view>
			<scroll-view scroll-y="true" :style="{'height': wh + 'px'}" class="cate-right">
				<view class="product-item" v-for="(item, i) in cateRightList" :key="i">
					<view class="title"> /{{ item.cat_name }}/</view>
					<view class="cate-product">
						<view class="item-box" v-for="(item2, i2) in item.children" :key="i2" @click="jumpGoodsList(item2)">
							<image :src="item2.cat_icon"></image>
							<text>{{item2.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		mixins: [badgeMix],
		data() {
			return {
				wh: 0,
				cateLeftList: [],
				active: 0,
				cateRightList: [],
				scrollTop: 0
			};
		},
		onLoad() {
			// 获取当前设备的一些信息
			const sysInfo = uni.getSystemInfoSync()
			this.wh = sysInfo.windowHeight - 50 // 因为有搜索区域占了50px
			this.getCateLeft()
		},
		methods: {
			async getCateLeft(){
				const {data} = await uni.$http.get('/api/public/v1/categories')
				if(data.meta.status !== 200) return uni.$showMsg()
				this.cateLeftList = data.message
				this.cateRightList = data.message[0].children
			},
			choice(item, index) {
				this.active = index
				const arr = this.cateLeftList.filter(v => item.cat_id === v.cat_id) || []
				this.cateRightList = arr[0].children
				// 切换左侧菜单时，让子分类列表内容回到顶部
				this.scrollTop = this.scrollTop === 0 ? 1 : 0 
			},
			jumpGoodsList(item) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?cid=' + item.cat_id
				})
			},
			gotoSearch() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
.cate-container {
	display: flex;
	.cate-left {
		width: 120px;
		.left-item {
			background-color: #efefef;
			text-align: center;
			line-height: 60px;
			font-size: 12px;
		}
		.active {
			background-color: #ffffff;
			position: relative;
		}
		.active::before {
			content: '';
			display: block;
			background-color: #C00000;
			width: 3px;
			height: 30px;
			position: absolute;
			top: 50%;
			left: 0;
			transform: translateY(-50%);
		}
	}
	.cate-right {
		.product-item {
			margin: 15px 0;
			.title {
				text-align: center;
				font-weight: bold;
				font-size: 12px;
			}
			.cate-product {
				display: flex;
				flex-wrap: wrap;
				.item-box {
					width: 33.33%;
					display: flex;
					flex-direction: column;
					align-items: center;
					image {
						width: 60px;
						height: 60px;
					}
				}
			}
		}
	}
}
</style>
